<?php
	/**
	 * 用户中心
	 * @author Administrator
	 */
	class userControl extends Control{
		public function __auto(){
			indexControl::get_citys_view();
			houseControl::auto_update_orderdata();
			if(!isset($_SESSION['user']) || empty($_SESSION['user']) || !is_array($_SESSION['user'])){
				go(__WEB__.'/index/login_reg/login');
			}
		}
		public function index(){
			$this->orderlist();
		}
		//用户中心首页
		public function orderlist (){
			$ispay = isset($_GET['num'])&&!empty($_GET['num'])?(int)$_GET['num']:0;
			$where_ispay = $ispay == 0?'': " ispay = '0'";
			$this->assign('checkbox',$ispay);
			if(!isset($_SESSION['user']) || empty($_SESSION['user'])){
				$this->error('您还没有登陆!');
			}
			$user = $_SESSION['user'];
			if(isset($_SESSION['house']) && isset($_SESSION['fei']) && isset($_SESSION['order'])){
				$house = $_SESSION['house']; //房屋信息
				$fei = $_SESSION['fei']; //费用信息
				$order = $_SESSION['order'];
				$db = M("order");
				$data = array();
				$data['uid'] = $user['uid'];
				$data['hid'] = $house['id'];
				$data['xiadantime'] = $order['xiadan'];
				$data['starttime'] = $order['startdate'];
				$data['endtime'] = $order['enddate'];
				$data['price'] = $fei['money'];
				$data['daynum'] = $fei['daynum'];
				$data['lianxiname'] = $order['v_contactname'];
				$data['lianximobile'] = $order['v_contactmobile'];
				$data['ruzhuname'] = $order['v_checkinname'];
				$data['ruzhumobile'] = $order['v_checkinmobile'];
				$data['lianxiemail'] = $order['v_contactemail'];
				$data['lianxitel'] = $order['v_contacttel'];
				$data['contract'] = $order['v_contract'];
				$data['liuyan'] = $order['liuyan'];
				$data['ispay'] = 0;
				$data['house_thumb'] = $house['thumb']; //房屋缩略图
				$data['house_title'] = $house['title']; //房屋标题
				$data['house_address'] = $house['address'];
				$data['house_geju'] = $house['geju'];
				$db->add($data);
				$this->session_del('house');
				$this->session_del("order");
				$this->session_del("fei");
			}
			$db = M("order");
			$rows = $db->where("uid = '".$user['uid']."'")->where($where_ispay)->all();
			if(isset($data)){
				$rows[count($rows)] = $data;
			}
			$rows = $rows?$rows:array();
			$this->assign('orders',$rows);
			$this->display();
		}
		//订单详情
		public function orderdetail(){
			$oid = isset($_GET['oid']) && !empty($_GET['oid'])?(int)$_GET['oid']:false;
			if($oid === false){
				$this->error("参数错误");
			}
			$db = M("order");
			$row = $db->where("oid = '".$oid."'")->find();
			$row = $row?$row:array();
			$this->assign('order',$row);
			$this->display();
		}
		//点评
		public function commentlist(){
			$user = $_SESSION['user'];
			$db = M("order");
			$rows = $db->where(" uid = '".$user['uid']."'")->where("status = 1")
			//->where(" ispay = 1")->where(" endtime <'".time()."'")
			->all();
			$this->assign("orders",$rows);
			$this->display();
		}
		//用户评论
		public function comment(){
			$user = $_SESSION['user'];
			$db = M("order");
			$rows = $db->where(" uid = '".$user['uid']."'")->find();
			$this->assign("order",$rows);
			$this->display();
		}
		//用户资料
		public function userinfo(){
			$db = M("user");
			P($_SESSION);
			$user = $_SESSION['user'];
			$userinfo = $db->where(" uid = '".$user['uid']."'")->find();
			$this->assign("user",$userinfo);
			$this->display();
		}
		//用户资料修改
		public function userinfo_modify(){
			$_POST['birth'] = strtotime(implode('-',$_POST['birth1']));
			unset($_POST['birth1']);
			$db = M("user");
			$db->update();
			$this->success("修改成功",__CONTROL__.'/orderlist');
		}
		//我的积分
		public function credits(){
			$this->display();
		}
		//修改密码
		public function changepassword(){
			$this->display();
		}
		//执行密码修改
		public function changepassword_exe(){
			$uid = $_SESSION['user']['uid'];
			if($_POST['oldpassword'] == $_POST['newpassword']){
				$this->error("密码没有修改");
			}else{
				$db = M("user");
				$user = $db->where(" uid = '".$uid."'")->find();
				if($user['password'] != md5($_POST['oldpassword'])){
					$this->error('原密码不正确');
				}else{
					if($_POST['newpassword'] != $_POST['repnewpassword']){
						$this->error("两次输入的密码不正确");
					}else{
						if($db->where(" uid = '".$uid."'")->update(array('password'=>md5($_POST['newpassword'])))){
							$this->success("修改成功");
						}else{
							$this->error("修改失败");
						}
					}
				}
			}
		}
	}

?>